package com.example.liquibasespringboot.conf.liquibase;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import javax.sql.DataSource;

/**
 * 注意如果有对象需要依赖数据库表，切ioc默认注入顺序在liquibase之前，需要给改对象的配置类添加
 * @DependsOn("liquibase")注解
 * 手写配置文件后yml中配置失效
 */

@Configuration
public class LiquibaseConfig {

    @Bean("db2DataSource")
    //@ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource getDb2DataSource(){
        return DataSourceBuilder.create()
                .driverClassName("com.mysql.cj.jdbc.Driver")
                .password("Leoz1012")
                .username("root")
                .url("jdbc:mysql://1.116.56.139:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8")
                .build();
    }

    @Bean("db1DataSource")
    //@ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource getDb1DataSource(){
        return DataSourceBuilder.create()
                .driverClassName("com.mysql.cj.jdbc.Driver")
                .password("Leoz1012")
                .username("root")
                .url("jdbc:mysql://1.116.56.139:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8")
                .build();
    }

    @Bean
    public SpringLiquibase liquibase1(@Qualifier("db1DataSource")DataSource dataSource) {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        //指定changelog的位置，这里使用的一个master文件引用其他文件的方式
        liquibase.setChangeLog("classpath:/liquibase/class-master.xml");
        liquibase.setDropFirst(false);
        //liquibase.setContexts("development,test,production");
        liquibase.setShouldRun(true);
        return liquibase;
    }

    @Bean
    public SpringLiquibase liquibase2(@Qualifier("db2DataSource") DataSource dataSource) {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        //指定changelog的位置，这里使用的一个master文件引用其他文件的方式
        liquibase.setChangeLog("classpath:/liquibase/master.xml");
        liquibase.setDropFirst(false);
        //liquibase.setContexts("development,test,production");
        liquibase.setShouldRun(true);
        return liquibase;
    }

}